package com.naiterui.ehp.ps.sms.factory.yunuo;

import java.util.Collections;
import java.util.Map;

import org.springframework.core.ParameterizedTypeReference;

import com.naiterui.common.redis.RedisUtil;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bp.utils.lang.ConfigUtil;
import com.naiterui.ehp.bp.utils.net.RestHttpUtil;
import com.naiterui.ehp.ps.sms.exception.ExceptionCodes;
import com.naiterui.ehp.ps.sms.exception.SmsBusinessException;
import com.naiterui.ehp.ps.sms.factory.yunuo.YunuoConstants.AccessTokenVO;
import com.naiterui.ehp.ps.sms.factory.yunuo.YunuoConstants.BaseRequestParam;
import com.naiterui.ehp.ps.sms.factory.yunuo.YunuoConstants.SendSmsParam;
import com.naiterui.ehp.ps.sms.factory.yunuo.YunuoConstants.SendSmsResult;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author gaoliangliang
 * @date 2020/12/29 下午6:45
 **/
@Slf4j
public class YunuoApi {

    /**
     * 获取雨诺接口api accessToken
     *
     * @param update
     * @return
     * @throws SmsBusinessException
     */
    public static String getAccessToken(boolean update) throws SmsBusinessException {

        if (!update) {
            String accessToken = RedisUtil.valueOps().getString(YunuoConstants.ACCESS_TOKEN_CACHE);
            if (StrUtil.isNotBlank(accessToken)) {
                return accessToken;
            }
        }

        ResponseVO<AccessTokenVO> accessTokenVO = RestHttpUtil.get()
            .url(String.format(YunuoConstants.TOKEN_API_URL, ConfigUtil.getString(YunuoConstants.APPID_KEY_CONFIG),
                ConfigUtil.getString(YunuoConstants.SECRET_KEY_CONFIG)))
            .execute(new ParameterizedTypeReference<ResponseVO<AccessTokenVO>>() {}).getBody();
        log.info("YunuoApi getAccessToken RESULT ; info = {}", JSONUtil.toJsonStr(accessTokenVO));
        if (accessTokenVO == null || accessTokenVO.getCode() != YunuoConstants.REQUEST_SUCCESS) {
            throw new SmsBusinessException(ExceptionCodes.SMS_YUNUO_ACCESS_TOKEN_ERROR);
        }
        String accessToken = accessTokenVO.getData().getAccess_token();
        RedisUtil.valueOps().setString(YunuoConstants.ACCESS_TOKEN_CACHE, accessToken, 55 * 60 * 2);

        return accessToken;
    }

    public static SendSmsResult singleSend(String phone, String templateId, Map<String, String> params)
        throws SmsBusinessException {

        ResponseVO<SendSmsResult> sendSmsResult = RestHttpUtil.post().url(YunuoConstants.MSG_SEND_API_URL)
            .body(JSONUtil.toJsonStr(BaseRequestParam.builder().token(getAccessToken(false))
                .data(SendSmsParam.builder().apply_code(templateId).phonenumjson(Collections.singletonList(phone))
                    .tempparamsjson(Collections.singletonList(params)).build())
                .build()))
            .execute(new ParameterizedTypeReference<ResponseVO<SendSmsResult>>() {}).getBody();
        if (sendSmsResult == null || sendSmsResult.getCode() != YunuoConstants.REQUEST_SUCCESS) {
            throw new SmsBusinessException(ExceptionCodes.SMS_YUNUO_SMS_SEND_ERROR);
        }
        return sendSmsResult.getData();
    }
}
